/**
 * User: yd
 * Date: 2019/5/27
 * Time: 9:48
 **/
//axios封装
import loadaxios from 'axios'
//import { Toast,Indicator } from 'mint-ui'
import {Loading, Message} from 'element-ui'
let loading;
function showMessage(txt,type){
	Message({
		showClose: true,
		message: txt,
		type: type
	})
}

// axios 配置
loadaxios.defaults.timeout = 5000;

// http request 拦截器
loadaxios.interceptors.request.use(
	config => {
		loading = Loading.service({fullscreen: true})
		return config;
	},
	err => {
		loading.close();
		showMessage('请求失败','warning');
		return Promise.reject(err);
	});

// http response 拦截器
loadaxios.interceptors.response.use(
	response=> {
		loading.close();
		return response;
	},
	err => {
		loading.close();
		if (err && err.response) {
			switch (err.response.status) {
				case 400:
					showMessage('请求错误','error');
					break
				case 401:
					showMessage('未授权，请登录','warning');
					break
				case 403:
					showMessage('拒绝访问','warning');
					break
				case 404:
					showMessage(`请求地址出错: ${err.response.config.url}`,'error');
					break
				case 408:
					showMessage('请求超时','warning');
					break
				case 500:
					showMessage('服务器内部错误','error');
					break
				case 501:
					showMessage('服务未实现','error');
					break
				case 502:
					showMessage('网关错误','error');
					break
				case 503:
					showMessage('服务不可用','warning');
					break
				case 504:
					showMessage('网关超时','warning');
					break
				case 505:
					showMessage('HTTP版本不受支持','warning');
					break
				default:
			}
		}
		return Promise.reject(err)
	});

export default loadaxios;